while

 

While begins a loop that executes a block of commands, as long as a given reporter returns True. If the reporter reports False , the loop is exited. It takes the form while [ reporter ] [ commands ]. For example, in

ask turtle 0 [ 
    while [ any? Other turtles-here ] [ 
        forward 1] ]

turtle 0 would keep moving forward until it comes to a patch that has no other turtles on it, then it would stop.

Note: while loops and other loops are not used often in NetLogo models.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the while primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the while primitive:
 
 
Similar primitives:
if

Conditionally run a command.

Read more
if-else

execute specific code if a condition is true, otherwise, execute other code

Read more
repeat

allows you to execute any set of commands n amount of times

Read more
tick

Advance the tick counter by 1.

Read more
 
Learn another primitive